package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.impl.EmployeeDaoImpl;
import Domain.Employee;

@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		String type=request.getParameter("type");
		EmployeeDaoImpl epdi=new EmployeeDaoImpl();
		Employee emp=null;
		if(type.equals("delete")) {
			emp=epdi.findEmployee(id);
			epdi.DeleteEmployee(emp);
			request.getRequestDispatcher("http://localhost:8080/Homework/showEmployee.jsp").forward(request, response);
		}
		else if(type.equals("update")){
			emp.setName(request.getParameter("empname"));
			emp.setSex(request.getParameter("empsex"));
			emp.setDapartment(request.getParameter("empdapartment"));
			emp.setId(id);
			emp.setType(request.getParameter("emptype"));
			emp.setYear(Integer.parseInt(request.getParameter("empyear")));
			emp.setAge(Integer.parseInt(request.getParameter("empage")));
			epdi.UpdateEmployee(emp);
			request.getRequestDispatcher("http://localhost:8080/Homework/showEmployee.jsp").forward(request, response);
		}
		else if(type.equals("add")) {
			emp.setName(request.getParameter("empname"));
			emp.setSex(request.getParameter("empsex"));
			emp.setDapartment(request.getParameter("empdapartment"));
			emp.setId(id);
			emp.setType(request.getParameter("emptype"));
			emp.setYear(Integer.parseInt(request.getParameter("empyear")));
			emp.setAge(Integer.parseInt(request.getParameter("empage")));
			epdi.AddEmployee(emp);
			request.getRequestDispatcher("http://localhost:8080/Homework/showEmployee.jsp").forward(request, response);
		}
	}
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
